﻿//------------------------------------------------------------
// Game Framework
// Copyright © 2013-2021 Jiang Yin. All rights reserved.
// Homepage: https://gameframework.cn/
// Feedback: mailto:ellan@gameframework.cn
//------------------------------------------------------------

using GameFramework;
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityGameFramework.Runtime;

namespace Adventure
{
    public class AchievementForm : UGuiForm
    {
        public string[] xmlNameButtonText;
        public string[] xmlNameInfoText;

        public RectTransform contentRect;
        private MenuForm menuForm;
        public const string Achievement = "Achievement.";
        protected override void OnInit(object userData)
        {
            base.OnInit(userData);

            transform.FindTransform<UIButton>("MaskPanel").onClick.AddListener(() => { PlayUISound(10001); Close(); });

            xmlNameButtonText = new string[] { "ButtonText1", "ButtonText2", "ButtonText3", };

            xmlNameInfoText = new string[] { "InfoText1", "InfoText2", "InfoText3", "InfoText4",
                "InfoText5", "InfoText6", "InfoText7", "InfoText8", "InfoText9","InfoText10",  };
            //可以设定奖励到底奖励什么

            if (userData != null)
            {
                menuForm = (MenuForm)userData;
            }
            contentRect = transform.FindTransform<RectTransform>("Content");

            for (int i = 0; i < contentRect.childCount; i++)
            {
                contentRect.GetChild(i).Find("InfoText").GetComponent<Text>().text = GameEntry.Localization.GetString(Achievement + xmlNameInfoText[i]);

                var button = contentRect.GetChild(i).Find("ButtonText").GetComponent<UIButton>();

                button.onClick.AddListener(() =>
                {
                    string arg1 = button.GetComponent<Text>().text;
                    string arg2 = GameEntry.Localization.GetString(Achievement + xmlNameButtonText[1]);
                    if (arg1==arg2)
                    {
                        PlayUISound(10001);
                        GameEntry.Setting.SetBool(Achievement + "InfoText" + button.transform.parent.name, true);

                        GameEntry.ArchiveData.Data.Gold += GetTaskAWarldGold(int.Parse(button.transform.parent.name) );
                      
                        ((MenuForm)userData).UpdatePlayerData();//因为不会重新打开，所以需要调用刷新
                        UpdateView();
                    }              
                });

            }
        }
        protected override void OnOpen(object userData)
        {
            base.OnOpen(userData);

            UpdateView();

            transform.FindTransform<ScrollRect>("Scroll View").verticalNormalizedPosition = 1;
        }

       
        private void UpdateView()
        {

            for (int i = 0; i < contentRect.childCount; i++)
            {
                bool has = GameEntry.Setting.HasSetting(Achievement + xmlNameInfoText[i]);

                Text t = contentRect.GetChild(i).Find("ButtonText").GetComponent<Text>();

                string buttonContent;
                if (has)
                {
                    bool state = GameEntry.Setting.GetBool(Achievement + xmlNameInfoText[i]);

                    if (state)
                    {
                        buttonContent = GameEntry.Localization.GetString(Achievement + xmlNameButtonText[2]);//已领取
                        t.color = new Color(1, 1, 0);
                    }
                    else
                    {
                        buttonContent = GameEntry.Localization.GetString(Achievement + xmlNameButtonText[1]);//待领取
                        t.color = new Color(0, 1, 0);
                    }

                }
                else
                {
                    buttonContent = GameEntry.Localization.GetString(Achievement + xmlNameButtonText[0]);//为达成
                    t.color = new Color(1, 0, 0);
                }
                t.text = buttonContent;
               
            }

        }

        /// <summary>
        /// 有key代表条件满足，true代表已领取
        /// </summary>
        /// <param name="index"></param>
        /// <param name="b"></param>
        public static void Finish_Task(int index, bool b)
        {
            if (GameEntry.Setting.HasSetting(Achievement + "InfoText" + index))
            {
                return;
            }
            GameEntry.Setting.SetBool(Achievement + "InfoText" + index, b);
        }

        /// <summary>
        /// 传入第几个任务1 2 3 4 5 6 7 8 9 10
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public static int GetTaskAWarldGold(int index)
        {
            if (index == 1) return 1;
            if (index == 2) return 10;
            if (index == 3) return 20;
            if (index == 4) return 30;
            if (index == 5) return 40;
            if (index == 6) return 50;
            if (index == 7) return 10;
            if (index == 8) return 10;
            if (index == 9) return 10;
            if (index == 10) return 10;

            return 0;
        }

    }
}
